package com.neusoft.yiyang.diet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.yiyang.common.entity.diet.*;
import com.neusoft.yiyang.common.entity.diet.vto.DietPlanRequest;
import com.neusoft.yiyang.common.entity.diet.vto.FoodItem;
import com.neusoft.yiyang.common.entity.diet.vto.FoodItemDetail;
import com.neusoft.yiyang.diet.mapper.*;
import com.neusoft.yiyang.diet.service.MealTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MealTemplateServiceImpl implements MealTemplateService {

    @Autowired
    private MealTemplateMapper mealTemplateMapper;

    @Autowired
    private FoodTypeRecordMapper foodTypeRecordMapper;

    @Autowired
    private MealTemplateItemMapper mealTemplateItemMapper;

    @Autowired
    private DietTagMapper dietTagMapper;

    @Autowired
    private FoodDictMapper foodDictMapper;

    @Override
    public void addMealTemplate(MealTemplate mealTemplate) {
        mealTemplateMapper.insert(mealTemplate);
    }

    @Override
    public List<MealTemplate> getAllMealTemplates() {
        return mealTemplateMapper.selectList(null);
    }

    @Override
    public MealTemplate getMealTemplateById(Integer templateId) {
        return mealTemplateMapper.selectById(templateId);
    }

    @Override
    public void updateMealTemplate(MealTemplate mealTemplate) {
        mealTemplateMapper.updateById(mealTemplate);
    }

    @Override
    public void deleteMealTemplate(Integer templateId) {
        mealTemplateMapper.deleteById(templateId);
    }

    @Override
    public void saveTemplate(DietPlanRequest dietPlanRequest) {
        Map<String, Map<String, FoodItem>> template = dietPlanRequest.getDietPlan();
        Date date = dietPlanRequest.getDate();
        String category = dietPlanRequest.getCategory(); //这个是正常，低糖。。。



        QueryWrapper<DietTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_name", category);
        List<DietTag> dietTags = dietTagMapper.selectList(queryWrapper);
        int tag_id = dietTags.get(0).getTagId();

        QueryWrapper<MealTemplate> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("meal_time", date);
        queryWrapper1.eq("tag_id", tag_id);
        List<MealTemplate> mealTemplates = mealTemplateMapper.selectList(queryWrapper1);
        for (MealTemplate mealTemplate : mealTemplates) {
            mealTemplateMapper.deleteById(mealTemplate.getTemplateId());
        }

        System.out.println("date: " + date);

        for (Map.Entry<String, Map<String, FoodItem>> outerEntry : template.entrySet()) {
            String mealType = outerEntry.getKey();
            MealTemplate mealTemplate = new MealTemplate();
            mealTemplate.setMealTime(date);
            mealTemplate.setMealType(category);
            mealTemplate.setMealType(mealType);
            mealTemplate.setTagId(tag_id);
            mealTemplateMapper.insert(mealTemplate);
            Map<String, FoodItem> innerMap = outerEntry.getValue();

            int templateId = mealTemplate.getTemplateId();
            // 遍历内层 Map
            for (Map.Entry<String, FoodItem> innerEntry : innerMap.entrySet()) {
                String foodType = innerEntry.getKey();
                FoodItem foodItem = innerEntry.getValue();
                if (foodItem != null) {
                    int limit = foodItem.getLimit();
                    FoodTypeRecord foodTypeRecord = new FoodTypeRecord();
                    foodTypeRecord.setTemplateId(templateId);
                    foodTypeRecord.setCategory(foodType);
                    foodTypeRecord.setNumber(limit);
                    foodTypeRecordMapper.insert(foodTypeRecord);
                    List<Integer> foodIds = foodItem.getSelected();
                    for (Integer foodId : foodIds) {
                        MealTemplateItem mealTemplateItem = new MealTemplateItem();
                        mealTemplateItem.setFoodTypeRecordId(foodTypeRecord.getFoodTypeRecordId());
                        mealTemplateItem.setFoodId(foodId);
                        mealTemplateItemMapper.insert(mealTemplateItem);
                    }
                }
            }
        }
    }

    @Override
    public Map<String, Map<String, FoodItem>> getTemplate(Date date, String category) {
        Map<String, Map<String, FoodItem>> ret = new HashMap<>();
        // 使用 Java 9+ 的 List.of() 方法初始化列表
        List<String> mealTypes = List.of("breakfast", "lunch", "dinner");
        List<String> categories = List.of("staple", "mainDish", "sideDish", "drink", "fruit");

        for (String mealType1 : mealTypes) {
            // 为每个餐食类型创建一个新的内层 Map
            Map<String, FoodItem> foodItems = new HashMap<>();
            for (String category1 : categories) {
                foodItems.put(category1, new FoodItem());
            }
            // 将内层 Map 放入外层 Map
            ret.put(mealType1, foodItems);
        }


        QueryWrapper<DietTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_name", category);
        List<DietTag> dietTags = dietTagMapper.selectList(queryWrapper);
        int tag_id = dietTags.get(0).getTagId();

        QueryWrapper<MealTemplate> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("meal_time", date);
        queryWrapper1.eq("tag_id", tag_id);
        List<MealTemplate> mealTemplates = mealTemplateMapper.selectList(queryWrapper1);
        for (MealTemplate mealTemplate : mealTemplates) {
            String mealType = mealTemplate.getMealType();
            QueryWrapper<FoodTypeRecord> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("template_id", mealTemplate.getTemplateId());
            List<FoodTypeRecord> foodTypeRecords = foodTypeRecordMapper.selectList(queryWrapper2);
            for (FoodTypeRecord foodTypeRecord : foodTypeRecords) {
                String foodType = foodTypeRecord.getCategory();
                Integer limit = foodTypeRecord.getNumber();
                QueryWrapper<MealTemplateItem> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("food_type_record_id", foodTypeRecord.getFoodTypeRecordId());
                List<MealTemplateItem> mealTemplateItems = mealTemplateItemMapper.selectList(queryWrapper3);
                ret.get(mealType).get(foodType).setLimit(limit);
                for (MealTemplateItem mealTemplateItem : mealTemplateItems) {
                    ret.get(mealType).get(foodType).getSelected().add(mealTemplateItem.getFoodId());
                }
            }
        }
        System.out.println(ret);
        return ret;

    }

    @Override
    public Map<String, Map<String, FoodItemDetail>> getTemplateDetail(Date date, String category) {
        Map<String, Map<String, FoodItemDetail>> ret = new HashMap<>();
        // 使用 Java 9+ 的 List.of() 方法初始化列表
        List<String> mealTypes = List.of("breakfast", "lunch", "dinner");
        List<String> categories = List.of("staple", "mainDish", "sideDish", "drink", "fruit");

        for (String mealType1 : mealTypes) {
            // 为每个餐食类型创建一个新的内层 Map
            Map<String, FoodItemDetail> foodItems = new HashMap<>();
            for (String category1 : categories) {
                foodItems.put(category1, new FoodItemDetail());
            }
            // 将内层 Map 放入外层 Map
            ret.put(mealType1, foodItems);
        }


        QueryWrapper<DietTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_name", category);
        List<DietTag> dietTags = dietTagMapper.selectList(queryWrapper);
        int tag_id = dietTags.get(0).getTagId();

        QueryWrapper<MealTemplate> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("meal_time", date);
        queryWrapper1.eq("tag_id", tag_id);
        List<MealTemplate> mealTemplates = mealTemplateMapper.selectList(queryWrapper1);
        for (MealTemplate mealTemplate : mealTemplates) {
            String mealType = mealTemplate.getMealType();
            QueryWrapper<FoodTypeRecord> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("template_id", mealTemplate.getTemplateId());
            List<FoodTypeRecord> foodTypeRecords = foodTypeRecordMapper.selectList(queryWrapper2);
            for (FoodTypeRecord foodTypeRecord : foodTypeRecords) {
                String foodType = foodTypeRecord.getCategory();
                Integer limit = foodTypeRecord.getNumber();
                QueryWrapper<MealTemplateItem> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("food_type_record_id", foodTypeRecord.getFoodTypeRecordId());
                List<MealTemplateItem> mealTemplateItems = mealTemplateItemMapper.selectList(queryWrapper3);
                ret.get(mealType).get(foodType).setLimit(limit);
                List<Integer> foodIds = new ArrayList<>();
                for (MealTemplateItem mealTemplateItem : mealTemplateItems) {
                    foodIds.add(mealTemplateItem.getFoodId());
                }
                if (!foodIds.isEmpty()) {
                    QueryWrapper<FoodDict> queryWrapper4 = new QueryWrapper<>();
                    queryWrapper4.in("food_id", foodIds);
                    List<FoodDict> foodDicts = foodDictMapper.selectList(queryWrapper4);
                    ret.get(mealType).get(foodType).setSelected(foodDicts);
                }

            }
        }
        System.out.println(ret);
        return ret;
    }
}
